package Server.DAO;

import Server.domain.Future;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;

public class FutureDAO {
	
	private static String updateFutureNumberSql="update futures set future_number=? where user_id=? and future_name=?";
    private static String insertFutureDealSql="INSERT INTO futures(user_id,future_name,future_number,future_deal_point)" +
    		" VALUES(?,?,?,?)";
    private static String findDealByUserIdAndFutureNameSql ="select * from futures where user_id=? and future_name=?";
    private static String deleteDealByUserIdAndFutureNameSql ="delete from futures where user_id =? and future_name =?";
    
    public double updateFutureNumber(Future future,double dealFutureNumber,int index){
    	double oldPrice = future.getFutureDealPoint();
    	Connection connection = DBUtil.getConnection();
    	try {
			PreparedStatement pStatement = connection.prepareStatement(updateFutureNumberSql);
			double newFutureNumber = future.getFutureNumber()+dealFutureNumber*index;
			//判断如果数量为0 ，则删除记录
			if(newFutureNumber<=0){
				deleteDealByUserIdAndFutureName(future);
				oldPrice = -1;
			}else{
				pStatement.setDouble(1, newFutureNumber);
				pStatement.setInt(2, future.getUserId());
				pStatement.setString(3, future.getFutureName());
				pStatement.executeUpdate();	
			}
			DBUtil.closePreparedStatement(pStatement);
			DBUtil.closeConnection(connection);
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
    	return oldPrice;
    	
    }
    
    public void insertFutureDeal(Future future){
    	Connection connection = DBUtil.getConnection();
    	try {
			PreparedStatement pStatement =
					connection.prepareStatement(insertFutureDealSql);
			pStatement.setInt(1, future.getUserId());
			pStatement.setString(2,future.getFutureName());
			pStatement.setDouble(3, future.getFutureNumber());
			pStatement.setDouble(4,future.getFutureDealPoint());
			pStatement.executeUpdate();
			DBUtil.closePreparedStatement(pStatement);
			DBUtil.closeConnection(connection);
		} catch (SQLException e) {
			e.printStackTrace();
		}   	
    	
    }
    
    public Future findDealByUserIdAndFutureName(Future future){
    	Future findFuture = null;
    	Connection connection = DBUtil.getConnection();
    	try {
			PreparedStatement pStatement = 
					connection.prepareStatement(findDealByUserIdAndFutureNameSql);
			pStatement.setInt(1, future.getUserId());
			pStatement.setString(2, future.getFutureName());
			ResultSet resultSet = pStatement.executeQuery();
			if(resultSet.next()){
				findFuture = getFutureFromResultSet(resultSet);
			}
			DBUtil.closeResultSet(resultSet);
			DBUtil.closePreparedStatement(pStatement);
			DBUtil.closeConnection(connection);
		} catch (SQLException e) {
			e.printStackTrace();
		}   	
    	return findFuture;
    }
    
    public void deleteDealByUserIdAndFutureName(Future future){
    	Connection connection = DBUtil.getConnection();
    	try {
			PreparedStatement pStatement = connection.prepareStatement(deleteDealByUserIdAndFutureNameSql);
			pStatement.setInt(1, future.getUserId());
			pStatement.setString(2, future.getFutureName());
			pStatement.executeUpdate();
			DBUtil.closePreparedStatement(pStatement);
			DBUtil.closeConnection(connection);
		} catch (SQLException e) {
			e.printStackTrace();
		}
    	
    }
    
    private Future getFutureFromResultSet(ResultSet resultSet) throws SQLException{
    	Future future = new Future();
    	future.setFutureId(resultSet.getInt(1));
    	future.setUserId(resultSet.getInt(2));
    	future.setFutureName(resultSet.getString(3));
    	future.setFutureNumber(resultSet.getDouble(4));
    	future.setFutureDealPoint(resultSet.getDouble(5));
    	return future;
    }
    
    //如果User已经购买某股指期货，只需执行updateFutureNumber操作；如果从未购买过则需执行insertFutureDeal操作；
    public void buy(Future future){
    	Future selectFuture = findDealByUserIdAndFutureName(future);
    	if(selectFuture!=null){
    		System.out.println(selectFuture.toString());
    		updateFutureNumber(selectFuture,future.getFutureNumber(),1);//更新表中已存在的
    	}else{
    		insertFutureDeal(future);//插入新建的Future
    	}
    }
    
    //执行updateFutureNumber操作，返回当时的成交价oldPrice
    public double sell(Future future){
    	Future selectFuture = findDealByUserIdAndFutureName(future);
    	double oldPrice =  updateFutureNumber(selectFuture,future.getFutureNumber(),-1);
    	return oldPrice;
    }
    
   /* public static void main (String[] args){
    	
    	FutureDAO futureDao = new FutureDAO();
    	Future future1 = new Future(1,"IF1412",20.0,1450.0);
    	Future future2 = new Future(1,"IF1412",40.0,1450.0);
    	
    	//Test buy()方法中的insertDeal+findDeal
    	//futureDao.buy(future1);
    	
    	//Test buy()方法中的updateDeal+findDeal
    	//futureDao.buy(future2);
    	
    	//Test sell()updateDeal()减去一部分
    	//futureDao.sell(future1);
    	
    	//Test sell() 减去全部，deleteDeal（）
    	//futureDao.sell(future2);
    	
    	
    }*/
}
